/**
 * 
 */
package com.jimet.authorization;

import java.sql.ResultSet;

import com.jimet.db.JimetAuthorizationDB;

/**
 * @author Administrator
 *
 */
public class JimetAuthorization {

	private int id = -1;
	private String shortCode;
	private String description;
	private String authType;
	

	/**
	 * @param id
	 * CONSTRUCTS a jimetauthorization with the given id
	 */
	public JimetAuthorization(int id) {
	this.id = id;
	}
	
	
	/**
	 * constructer
	 */
	public JimetAuthorization() {
	}


	/**
	 * @param strAuthId
	 * @throws Exception
	 * sets all the attributes of the jimetauthorization with the given id
	 */
	public void load(String strAuthId) throws Exception
	{
		JimetAuthorizationDB jimdb = new JimetAuthorizationDB();	
		
		ResultSet rs = jimdb.getAuthByID(Integer.parseInt(strAuthId));
		while(rs.next())
		{
			this.setId(rs.getInt(1));
			this.setShortCode(rs.getString(2));
			this.setDescription(rs.getString(3));
			this.setAuthType(rs.getString(4));

		}
		
		jimdb.disconnect();

	}
	public void loadByName(String strAuthName) throws Exception
	{
		JimetAuthorizationDB jimdb = new JimetAuthorizationDB();	
		
		ResultSet rs = jimdb.getAuthByName(strAuthName);
		while(rs.next())
		{
			this.setId(rs.getInt(1));
			this.setShortCode(rs.getString(2));
			this.setDescription(rs.getString(3));
			this.setAuthType(rs.getString(4));

		}
		
		jimdb.disconnect();

	}	
	
	/**
	 * @return
	 * @throws Exception
	 * if authid = -1 inserts a new auth else updates the auth with id
	 */
	public boolean store() throws Exception
	{
		JimetAuthorizationDB jimdb = new JimetAuthorizationDB();
		boolean queryOK;
		if(this.id == -1)
			queryOK = jimdb.addAuth(this.getShortCode(),this.getDescription(),this.getAuthType());
		else 
			queryOK = jimdb.updateAuth(this.id, this.getShortCode(),this.getDescription(),this.getAuthType());
		jimdb.disconnect();
		return queryOK;
	}

	
	/**
	 * @return
	 * @throws Exception
	 * phisicaly deletes the id from usr_right
	 */
	public boolean delete() throws Exception
	{
		JimetAuthorizationDB jimdb = new JimetAuthorizationDB();
		boolean queryOK;
			queryOK = jimdb.deleteAuth(this.id);
		jimdb.disconnect();
		return queryOK;
	}


	/**
	 * @return Returns the description.
	 */
	public String getDescription() {
		return description;
	}




	/**
	 * @param description The description to set.
	 */
	public void setDescription(String description) {
		this.description = description;
	}




	/**
	 * @return Returns the iD.
	 */
	public int getId() {
		return this.id;
	}




	/**
	 * @param id The iD to set.
	 */
	public void setId(int id) {
		this.id = id;
	}




	/**
	 * @return Returns the name.
	 */
	public String getShortCode() {
		return this.shortCode;
	}




	/**
	 * @param name The name to set.
	 */
	public void setShortCode(String shortCode) {
		this.shortCode = shortCode;
	}

	/**
	 * @return Returns the authType.
	 */
	public String getAuthType() {
		return authType;
	}

	/**
	 * @param authType The authType to set.
	 */
	public void setAuthType(String authType) {
		this.authType = authType;
	}

	
	
	
}
